package com.hontech.bv.ui.fragment.heat

import android.view.Gravity
import android.widget.Button
import com.hontech.bv.R
import com.hontech.bv.conn.api.Heat
import com.hontech.bv.model.heat.CtrlValue
import com.hontech.bv.model.heat.HeatStatus
import com.hontech.bv.task.appScope
import com.hontech.bv.ui.AutoHideKeyActivity
import com.hontech.bv.ui.view.SwitchCtrlView
import com.hontech.bv.ui.window.MoveableWindow
import com.hontech.bv.ui.wrapDialog
import com.hontech.bv.utils.dimenIntById
import kotlinx.coroutines.launch

private val width = dimenIntById(R.dimen.w800)
private val height = dimenIntById(R.dimen.h940)

private fun typeOf(type: Int): String {
    when (type) {
        1 -> return "开水加热器"
        2 -> return "蒸汽加热器"
        3 -> return "热水泵"
        4 -> return "清洗泵"
        5 -> return "热水阀"
        6 -> return "清洗阀"
        7 -> return "蒸汽泵"
        8 -> return "蒸汽阀"
        9 -> return "水桶1水泵"
        10 -> return "水桶2水泵"
        11 -> return "RO出水阀"
        12 -> return "RO进水阀"
        13 -> return "搅拌电机"
        14 -> return "夹管阀"
        else -> return "未知组件"
    }
}

private fun valueOf(value: Int): String {
    return if (value == 0) "打开" else "关闭"
}

class HeatPartsDialog : MoveableWindow(R.layout.dialog_heat_parts, width, height, false) {

    companion object {
        private var INSTANCE: HeatPartsDialog? = null

        fun show() {
            if (INSTANCE == null) {
                INSTANCE = HeatPartsDialog()
            }
            INSTANCE!!.show()
        }

        fun update(ctrl: CtrlValue) {
            INSTANCE?.update(ctrl)
        }
    }

    private val btnClose = view.findViewById<Button>(R.id.id_heat_parts_btn_close)

    private val scvWaterHeat = view.findViewById<SwitchCtrlView>(R.id.id_heat_parts_scv_water_heat)
    private val scvSteamHeat = view.findViewById<SwitchCtrlView>(R.id.id_heat_parts_scv_steam_heat)
    private val scvWaterPump = view.findViewById<SwitchCtrlView>(R.id.id_heat_parts_scv_water_pump)
    private val scvCleanPump = view.findViewById<SwitchCtrlView>(R.id.id_heat_parts_scv_clean_pump)
    private val scvWaterValve = view.findViewById<SwitchCtrlView>(R.id.id_heat_parts_scv_water_valve)
    private val scvCleanValve = view.findViewById<SwitchCtrlView>(R.id.id_heat_parts_scv_clean_valve)
    private val scvSteamPump = view.findViewById<SwitchCtrlView>(R.id.id_heat_parts_scv_steam_pump)
    private val scvSteamValve = view.findViewById<SwitchCtrlView>(R.id.id_heat_parts_scv_steam_valve)
    private val scvBucket1Pump = view.findViewById<SwitchCtrlView>(R.id.id_heat_parts_scv_bucket1_pump)
    private val scvBucket2Pump = view.findViewById<SwitchCtrlView>(R.id.id_heat_parts_scv_bucket2_pump)
    private val scvRoExitValve = view.findViewById<SwitchCtrlView>(R.id.id_heat_parts_scv_ro_exit_valve)
    private val scvRoEnterValve = view.findViewById<SwitchCtrlView>(R.id.id_heat_parts_scv_ro_enter_valve)
    private val scvStirMotor = view.findViewById<SwitchCtrlView>(R.id.id_heat_parts_scv_stir_motor)
    private val scvDropValve = view.findViewById<SwitchCtrlView>(R.id.id_heat_parts_scv_drop_valve)

    init {
        btnClose.setOnClickListener {
            dismiss()
            INSTANCE = null
        }

        scvWaterHeat.clickCb = { onCtrl(1, it) }
        scvSteamHeat.clickCb = { onCtrl(2, it) }
        scvWaterPump.clickCb = { onCtrl(3, it) }
        scvCleanPump.clickCb = { onCtrl(4, it) }
        scvWaterValve.clickCb = { onCtrl(5, it) }
        scvCleanValve.clickCb = { onCtrl(6, it) }
        scvSteamPump.clickCb = { onCtrl(7, it) }
        scvSteamValve.clickCb = { onCtrl(8, it) }
        scvBucket1Pump.clickCb = { onCtrl(9, it) }
        scvBucket2Pump.clickCb = { onCtrl(10, it) }
        scvRoExitValve.clickCb = { onCtrl(11, it) }
        scvRoEnterValve.clickCb = { onCtrl(12, it) }
        scvStirMotor.clickCb = { onCtrl(13, it) }
        scvDropValve.clickCb = { onCtrl(14, it) }
    }

    private fun onCtrl(type: Int, value: Boolean) {
        val ctrl = if (value) 0 else 1
        appScope.launch {
            wrapDialog("${typeOf(type)}", "${valueOf(ctrl)}") {
                Heat.partsCtrl(type, ctrl)
            }
        }
    }

    private fun show() {
        window.showAtLocation(AutoHideKeyActivity.parentView(), Gravity.CENTER, 0, 0)
        update(HeatStatus.status.ctrl())
    }

    private fun update(ctrl: CtrlValue) {
        scvWaterHeat.set(ctrl.waterHeatFlag)
        scvSteamHeat.set(ctrl.steamHeatFlag)
        scvWaterPump.set(ctrl.waterPumpFlag)
        scvCleanPump.set(ctrl.cleanPumpFlag)
        scvWaterValve.set(ctrl.waterValveFlag)
        scvCleanValve.set(ctrl.cleanValveFlag)
        scvSteamPump.set(ctrl.steamPumpFlag)
        scvSteamValve.set(ctrl.steamValveFlag)
        scvBucket1Pump.set(ctrl.bucket1PumpFlag)
        scvBucket2Pump.set(ctrl.bucket2PumpFlag)
        scvRoExitValve.set(ctrl.roExitValveFlag)
        scvRoEnterValve.set(ctrl.roEnterValveFlag)
        scvStirMotor.set(ctrl.stirMotorFlag)
        scvDropValve.set(ctrl.dropValveFlag)
    }
}


